<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/13
 * Time: 17:28
 * 评价
 */
namespace app\franchisee\controller;
use think\Db;
use think\Request;
class Evaluate extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //我收到的评价
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type');
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.evaluate_b_user_id'] = $user_id;
        if($type==1){
        $where['a.evaluate_review']  =1;
        }elseif ($type==2){
            $where['a.evaluate_review']  =2;
        }elseif ($type==3){
            $where['a.evaluate_review']  =3;
        }
        $res = Db::table('up_order_evaluate')->alias('a')
                ->join('up_u_user uuu','uuu.user_id= a.evaluate_p_user_id','left')
                ->where($where)->limit($limit,$num)
                ->field('uuu.user_username,uuu.user_image,a.*')
                ->order('a.evaluate_time desc')
                ->select();
        //统计多少数据
        $count = Db::table('up_order_evaluate')->alias('a')
            ->join('up_u_user uuu','uuu.user_id= a.evaluate_p_user_id','left')
            ->where($where)->count();
        $num = Db::table('up_order_evaluate')->alias('a')
            ->join('up_u_user uuu','uuu.user_id= a.evaluate_p_user_id','left')
            ->where($where)->sum('evaluate_star');

        $list['data'] = $res;
        if($count!=0){
            $list['star'] = $num/$count;
        }else{
            $list['star'] = $num;
        }
        return json(['status' => 200, 'msg' => $list]);
    }
    //我的评价
    public function index1(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.evaluate_p_user_id'] = $user_id;
        $res = Db::table('up_order_evaluate')->alias('a')
            ->join('up_u_user uuu','uuu.user_id= a.evaluate_p_user_id','left')
            ->where($where)->limit($limit,$num)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->order('a.evaluate_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_order')
                ->field('order_go_sheng,order_go_shi,order_to_sheng,order_to_shi')
                ->where('order_id',$vo['order_id'])
                ->find();
           $district  = Db::table('up_area')->where('district_id',$info['order_go_sheng'])->value('district');
           $district1 =Db::table('up_area')->where('district_id',$info['order_go_shi'])->value('district');
           $district2 = Db::table('up_area')->where('district_id',$info['order_to_sheng'])->value('district');
           $district3 =  Db::table('up_area')->where('district_id',$info['order_to_sheng'])->value('district');
            $vo['go_sheng'] = $district;
            $vo['go_shi'] = $district1;
            $vo['to_sheng'] = $district2;
            $vo['to_shi'] = $district3;
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //删除评价
    public function delEvaluate(){
        extract(input());
        $res = Db::table('up_order_evaluate')->where('evaluate_id',$evaluate_id)->delete();
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>0,'msg'=>'失败']);
        }
    }

    //待评价
    public function evaluate(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.order_jms_user'] = $user_id;
        $where['a.order_type'] = array('not in',array(3,4));
        $where['a.order_status'] = 8;
        $where['a.jms_pj'] = 1;
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->where($where)
            ->limit($limit,$num)
            ->field('a.order_dirver_user,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
            ->order('a.order_fb_time desc')
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    public function detail(){
        extract(input());
        $where['a1.evaluate_id'] = $evaluate_id;
        $res = Db::table('up_order_evaluate')->alias('a1')
            ->join('up_order a','a.order_id = a1.order_id','left')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->where($where)
            ->field('a.order_dirver_user,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi,a1.*')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    public function detailEvaluate(){
        extract(input());
        $where['a.order_id'] = $order_id;
        $res = Db::table('up_order')->alias('a')
            ->join('up_car_hwlx uch','uch.hwlx_id = a.hwlx_id','left')
            ->join('up_area ua','ua.district_id = a.order_go_sheng')
            ->join('up_area ua1','ua1.district_id = a.order_go_shi')
            ->join('up_area ua2','ua2.district_id = a.order_to_sheng')
            ->join('up_area ua3','ua3.district_id = a.order_to_shi')
            ->where($where)
            ->field('a.order_dirver_user,a.order_type,a.order_status,a.order_fb_time,a.cx_id as cx_name,uch.hwlx_name,
            a.order_cc,a.order_goods_ton,a.order_zc_time,
            a.order_id,ua.district as go_sheng,ua1.district as go_shi,
            ua2.district as to_sheng,ua3.district as to_shi')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //立即评价
    public function ljEvaluate(){
        extract(input());
        $evaluate_bq = input('param.evaluate_bq');
        $evaluate_star = input('param.evaluate_star');
        $evaluate_review = input('param.evaluate_review');
        $evaluate_content = input('param.evaluate_content');
        $evaluate_b_user_id = input('param.evaluate_b_user_id');
        $evaluate_img = input('param.evaluate_img');
        $order_id = input('param.order_id');

        $data['evaluate_p_user_id'] = $user_id;
        $data['evaluate_b_user_id'] = $evaluate_b_user_id;
        $data['order_id'] = $order_id;
        $data['evaluate_type'] = 2;
        $data['evaluate_content'] = $evaluate_content;
        $data['evaluate_star'] = $evaluate_star;
        $data['evaluate_bq'] = $evaluate_bq;
        $data['evaluate_review'] = $evaluate_review;
        $data['evaluate_img'] = $evaluate_img;
        $data['evaluate_time'] = date('Y-m-d H:i:s');
        $res = Db::table('up_order_evaluate')->insertGetId($data);
        //查询加盟商是否对这个订单是否评价
        $result = Db::table('up_order')->where('order_id',$order_id)->field('order_id,jms_pj')->find();
        if($result['jms_pj']==1){
           Db::table('up_order')->where('order_id',$order_id)->setField('jms_pj',2);
        }
        //查询这个加盟商和司机是否都评价完成
        $result1 = Db::table('up_order')->where('order_id',$order_id)->field('order_status,order_id,jms_pj,sj_pj')->find();
        if($result1['order_status']==8){//待评价
            if($result1['jms_pj']==2&&$result1['sj_pj']==2){//司机和加盟商都评价过
                Db::table('up_order')->where('order_id',$order_id)->setField('order_status',9);
            }
        }
        if($res){
           return json(['status'=>200,'msg'=>"成功"]);
        }else{
            return json(['status'=>100,'msg'=>"失败"]);
        }
    }
}